function $1(divName) { return document.getElementById(divName); };

function get(url, divName, parameters, onCompleteFunction, functionParams, scrollTop) {
	if(divName == null) divName = "content-area";
	$.get(url, parameters, function(data){
		$1(divName).innerHTML = data;
		$("#"+divName).find('script').each(function(){
			if (window.execScript){
				window.execScript(this.text || this.textContent || this.innerHTML || "");
			} else {
				eval.call( window, this.text || this.textContent || this.innerHTML || "" );
			}
			if(scrollTop != false){
				if (window != null)
					window.scrollTo(0,0);
			}
		});
		if(onCompleteFunction != null) {
			onCompleteFunction.call(this, functionParams, data);
		}
		hideLoading();
	});
};

function postForm(formObject, divId, onCompleteFunction, functionParams, scrollTop, customMsg) {
	if(divId == null) divId = "content-area";
	var opts = {
			target: "#"+divId,
			success: function(responseText)
			{
				if(scrollTop != false){
					if (window != null)
						window.scrollTo(0,0);
				}
				$(".errorLabel:first").each(function(i){
					if((this.scrollHeight > 0) && (window != null)){
						window.scrollTo(0,this.scrollHeight);
					}
				});
				if (onCompleteFunction != null) {
					onCompleteFunction.call(this, functionParams, responseText);
				}
				hideLoading();
			}
	};
	try{
		$(formObject).find('textarea[rel],input[rel]').each(function(i){
			if($.trim(this.value) == $.trim($(this).attr('rel'))){
				this.value = '';
			}
		});
		if(customMsg == null) customMsg = "Please wait loading ...";
		showLoading(customMsg);
		$("#"+formObject.name).ajaxSubmit(opts);
	}catch(e){alert(e);}
	return false;
}

function openConsole(domainName) {
	divId = 'console_'+domainName;
	if(document.getElementById(divId) == null) {
		var div = document.createElement('div');
		div.setAttribute('id', divId);
		document.body.appendChild(div)
	}
	get('/xen/openconsole?domain='+domainName,divId);
	return false;
};

leftMenuId='general'
function toggleLeftMenu(menuId) {
	if($('#'+leftMenuId)) {
		$('#'+leftMenuId).removeClass('selected')
	}
	if($('#'+menuId)) {
		$('#'+menuId).addClass('selected')
	}
	leftMenuId = menuId;
	return false;
};

function xenAction(action,div,loadingMsg,domainName) {
	showLoading(loadingMsg);
	get(action,div,"",reloadVMMenuList,domainName);
	return false;
};

reloadVMMenuList = function(domain) {
	get('/xen/vmListMenu?domain=' + domain,'xenMenu');
};

function loadAction(action,div,loadingMsg) {
	url = action.replace(/^.*#/, '');
	showLoading(loadingMsg);
	get(url,div);
	return false;
};

function showLoading(loadingMsg) {
	try{ 
		var obj = document.getElementById('content-right');
		var dm = findDimension(obj)
		var pos = findPos(obj)
		$('#loading_blk').removeAttr('style')
		if(loadingMsg) {
			$1('loadingmsg').innerHTML = loadingMsg;
		}
		var left = pos[0] + dm[0]/5;
		var top = pos[1] + dm[1]/3;
		style1 = "left:"+left+"px; top:"+top+"px;display: block"
		$('#loading_blk').attr('style',style1)
	}catch(e){alert(e)}
	return false;
};

function hideLoading() {
	try{ 
		$('#loading_blk').removeAttr('style')
		$('#loading_blk').attr('style','display: none');
		$1('loadingmsg').innerHTML = ''
		$('#blockout').removeAttr('style')
		$('#blockout').attr('style','display: none');
	}catch(e){}
	return false;
};

function findDimension(obj) {
	var wth = parseInt(obj.offsetWidth);
	var ht = parseInt(obj.offsetHeight);
	return [wth,ht];
};

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
		 curleft += obj.offsetLeft;
		 curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
};
